// pages/password/password.js
var app = getApp();
var ea = require('../../utils/each.js');
var time = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shuju:{},
    phone: '',
    code: '',
    password: '',
    fasongyanzhen: "发送验证码",
    kong: true,
    kongs: true,
    changepwd: app.data.url + 'zhongxue/teacher/changepwd',
    fasongurl: app.data.url + '/zhongxue/teacher/fasong',
    phoneUrl:[
      "/img/phone.png"
    ],
    safetyUrl:[
      "/img/yanzhengma.png"
    ],
    passwordUrl:[
      "/img/paw.png"
    ],
    footerUrl:[
      "/img/78339815933c647e2a1253504efdb90.png"
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  bindKeyInput: function (e) {
    // console.log(e);
    var value = e.detail.value;
    // console.log(value);
    this.setData({ phone: value });
  },
  bindKeyInputs: function (e) {
    var value = e.detail.value;
    this.setData({ code: value });
  },
  bindKeyInputss: function (e) {
    var value = e.detail.value;
    this.setData({ password: value });
  },
  fasong: function () {
    var kong = this.data.kong;
    if (!kong) {
      return
    }
    var phone = this.data.phone;
    if (!phone) {
      wx.showToast({
        title: '请填入手机号码',
      })
      return
    }
    this.setData({ kong: false });
    var user = wx.getStorageSync('user');
    var that = this;
    wx.showLoading({
      title: '发送中',
    })
    wx.request({
      url: that.data.fasongurl,
      data: { token: user.token, phone: phone },
      header: {},
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        var data = res.data;
        console.log(data);
        if (data.code == 200) {
          wx.showToast({
            title: '发送成功',
          })
          ea.TimeDown(60, function (time) {
            that.setData({ fasongyanzhen: time });
            if (time == 0) {
              that.setData({ kong: true, fasongyanzhen: "发送验证码" });
            }
          });
        } else {
          that.setData({ kong: true })
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
      },
      fail: function (res) {
        that.setData({ kong: true });
      },
      complete: function (res) {
        wx.hideLoading()
      },
    })
  },
  login: function () {
    var kongs = this.data.kongs;
    if (!kongs) {
      return
    }
    var tiaojian = {};
    var user = wx.getStorageSync('user');
    tiaojian.token = user.token;
    var phone = this.data.phone;
    var code = this.data.code;
    var password = this.data.password;
    if (!phone) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
      return
    }
    if (!code) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      })
      return
    }
    if (code.length < 4) {
      wx.showToast({
        title: '请输入4位验证码',
        icon: 'none'
      })
      return
    }
    if (!password) {
      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      })
      return
    }
    tiaojian.phone = phone;
    tiaojian.code = code;
    tiaojian.password = password;
    this.setData({ kongs: false });
    var that = this;
    wx.showLoading({
      title: '修改中',
    })
    wx.request({
      url: that.data.changepwd,
      data: tiaojian,
      header: {},
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        var data = res.data;
        if (data.code == 200) {
          wx.showToast({
            title: '修改成功'
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            })
          }, 1500)
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
      },
      fail: function (res) { },
      complete: function (res) {
        wx.hideLoading()
        that.setData({ kongs: true })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})